<template>
	<!-- 实名认证 -->
	<view class="main">
		<!-- 顶部 -->
		<view class="header">
			<uni-notice-bar background-color="rgb(233 243 247)" color="#2979FF" show-icon
				text="信息仅用于实名认证,士德物流保障您的信息安全" />
		</view>

		<!-- 内容中心 -->
		<uni-forms class="from-style" :model="mytupian">
			<view class="container">

				<view class="c-zi ">
					<uni-section title="请上传身份证照片" titleFontSize="16px" type="line">

						<view class="liangzhang">
							<uni-forms-item name="idcardzheng">
								<view class="bbb">
									<uni-file-picker file-mediatype="image" limit="1" file-extname="png,jpg"
										:image-styles="imageStyles" :auto-upload="false" @select="uploadface">
										<uni-icons type="plus-filled" size="30" color="blue"
											style="padding-left: 15px;"></uni-icons>
										<text>上传人像画</text>
									</uni-file-picker>
								</view>
							</uni-forms-item>

							<uni-forms-item name="idcardfan">
								<view class="bbb">
									<uni-file-picker file-mediatype="image" limit="1" file-extname="png,jpg"
										:image-styles="imageStyles" :auto-upload="false" @select="uploadback">
										<uni-icons type="plus-filled" size="30" color="blue"
											style="padding-left: 15px;"></uni-icons><br>
										<text>上传国徽面</text>
									</uni-file-picker>
								</view>
							</uni-forms-item>
						</view>

					</uni-section>
				</view>
			</view>

			<!-- 底部 -->
			<view class="bottom">
				<button type="primary" @click="tijiao()" class="button-style">提交审核</button>
			</view>
		</uni-forms>

	</view>
</template>

<script>
	export default {
		data( ) {
			return {
				imageStyles: {
					width: 185.25,
					height: 110,
				},
				mytupian: {
					idcardzheng: '',
					idcardfan: '',
					phone: '17635711209'
				},
				// 添加到数据库的信息
				tsRealName: {
					// address: '', //籍贯
					// birthDate: '', //出生日期
					// gender: '', //性别
					// IDNumber: '', //身份证号
					// name: '', //姓名
					// nationality: '', //民族
					// idCardFront: '', //身份证正面图片路径
					// issue: '', //签发机关
					// idCardOpposite: '', //身份证反面图片路径
					// validPeriod: '', //身份证有效期限
					// applyTime: '' //申请认证时间
				}
			};
		},
		methods: {
			//身份证正面上传
			uploadface( e ) {
				let that = this;
				let tiemr = new Date( );
				let address = tiemr.getFullYear( ) + '' + ( tiemr.getMonth( ) + 1 ) + '' + tiemr
					.getDate( );
				address = 'zskp/userHead/' + address + '/';
				var tempFilePaths = e.tempFilePaths[ 0 ];
				let str = e.tempFilePaths[ 0 ].substr( e.tempFilePaths[ 0 ].lastIndexOf( '.' ) );
				let nameStr = address + tiemr.getTime( ) + str;
				let ossUrl = 'http://localhost:8099/tsRealNameAuthentication/getIdCardFace';
				uni.uploadFile( {
					url: ossUrl, //输入你的bucketname.endpoint
					filePath: tempFilePaths,
					fileType: 'image',
					name: 'file',
					formData: {
						name: nameStr,
						key: nameStr,
						policy: 'eyJleHBpcmF0aW9uIjoiMjAyMy0wNi0yNlQwMjoyMjozOS4yMjJaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIzLTA2LTI2LyJdXX0=', // 输入你获取的的policy
						OSSAccessKeyId: 'LTAI5tJsR6uZnmEUoa4c5hzH', // 输入你的AccessKeyId
						success_action_status: '200', // 让服务端返回200,不然，默认会返回204
						signature: 'isMKvr1Wrwnd1zSpo/cgvCPKSNY=' // 输入你获取的的signature
					},
					success: res => {
						if ( res.statusCode == '200' ) {
							const result = JSON.parse( res.data )
							console.log(result.data1.idNumber)
							this.tsRealName.address = result.data1.address;
							this.tsRealName.birthDate = result.data1.birthDate;
							this.tsRealName.gender = result.data1.gender;
							this.tsRealName.idNumber = result.data1.idNumber;
							this.tsRealName.name = result.data1.name;
							this.tsRealName.nationality = result.data1.nationality;
							this.tsRealName.idCardFront = result.data1.idCardFront;
						} else {
							uni.showToast( {
								title: '上传失败',
								icon: 'none',
								duration: 1000
							} )
						}
					}
				} )
			},
			
			//身份证反面上传
			uploadback( e ) {
				let that = this;
				let tiemr = new Date( );
				let address = tiemr.getFullYear( ) + '' + ( tiemr.getMonth( ) + 1 ) + '' + tiemr
					.getDate( );
				address = 'zskp/userHead/' + address + '/';
				var tempFilePaths = e.tempFilePaths[ 0 ];
				let str = e.tempFilePaths[ 0 ].substr( e.tempFilePaths[ 0 ].lastIndexOf( '.' ) );
				let nameStr = address + tiemr.getTime( ) + str;
				let ossUrl = 'http://localhost:8099/tsRealNameAuthentication/getIdCardBack';
				uni.uploadFile( {
					url: ossUrl, //输入你的bucketname.endpoint
					filePath: tempFilePaths,
					fileType: 'image',
					name: 'file',
					formData: {
						name: nameStr,
						key: nameStr,
						policy: 'eyJleHBpcmF0aW9uIjoiMjAyMy0wNi0yNlQwMjoyMjozOS4yMjJaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIzLTA2LTI2LyJdXX0=', // 输入你获取的的policy
						OSSAccessKeyId: 'LTAI5tJsR6uZnmEUoa4c5hzH', // 输入你的AccessKeyId
						success_action_status: '200', // 让服务端返回200,不然，默认会返回204
						signature: 'isMKvr1Wrwnd1zSpo/cgvCPKSNY=' // 输入你获取的的signature
					},
					success: res => {
						if ( res.statusCode == '200' ) {
							const result = JSON.parse( res.data )
							this.tsRealName.issue = result.data1.issue;
							this.tsRealName.idCardOpposite = result.data1.idCardOpposite;
							this.tsRealName.validPeriod = result.data1.validPeriod;
						} else {
							uni.showToast( {
								title: '上传失败',
								icon: 'none',
								duration: 1000
							} )
						}
					}
				} )
			},

			// 提交审核
			async tijiao( ) {
				console.log( 11111 )
				const result = await uni.$http.post( "localhost:8099/tsRealNameAuthentication/insertRealName",
					this.tsRealName );
				console.log( this.tsRealName )
				const {
					data,
					code
				} = result.data
				if ( code == 100 ) {
					console.log( "添加成功" )
					this.tiao()
				} else if ( code == 101 ) {
					uni.showToast( {
						title: "信息填写错误，请重新填写信息",
						icon: 'none',
						duration: 1000
					} );
				} else if ( code == 102 ) {
					uni.showToast( {
						title: "账号已存在，请确认信息",
						icon: 'none',
						duration: 1000
					} );
				}
			},
	
			
			// 跳转
			tiao( ) {
				uni.switchTab( {
					url: '/pages/mine/mine'
				} )
			}
		}

	}
</script>

<style lang="scss">
	/* 最大容器 */
	.main {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
		background-color: Snow;
	}

	/* 头部容器 */
	.header {
		width: 100%;
		height: 100rpx;
		margin-bottom: -10px;
	}

	/* 中间容器 */
	.container {
		display: flex;
		flex-flow: column nowrap;
		justify-content: flex-start;
		align-content: space-evenly;
		width: 100%;
		height: 686px;
		overflow-y: scroll;
	}

	.c-zi {
		width: 95%;
		/* flex: 1; */
		margin: 0px auto;
		margin-bottom: -22px;
	}

	.yizhang {
		display: flex;
		width: 50%;
		height: 120px;
		// margin: 0 5px;
	}

	.liangzhang {
		display: flex;
		width: 100%;
		height: 120px;
		justify-content: space-evenly;
	}

	/* 底部容器 */
	.bottom {
		width: 100%;
		height: 120rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	/* 下一步按钮样式 */
	.button-style {
		margin: 0 auto;
		width: 80%;
		border-radius: 35px;
	}

	.aaa {
		display: flex;
		width: 100%;
		height: 100%;
		justify-content: center;
		align-items: center;
		background-color: whitesmoke;
		/* padding: 10px;
		margin: 0 5px; */
	}

	.bbb {
		display: flex;
		flex-flow: column nowrap;
		justify-content: center;
		align-items: center;
		background-color: whitesmoke;
	}
</style>